package com.burpee.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.service.MessageService;
import com.burpee.ui.Member;
import com.burpee.ui.Message;

@Controller
@RequestMapping("/message.spring")
public class MessageController {

	@Autowired
	MessageService messageService;

	@RequestMapping(params = "action=messagelist")
	public ModelAndView messagelist(HttpServletRequest request, HttpServletResponse response, Integer currentPage) {
		Member member = (Member)request.getSession().getAttribute("login");
		if (currentPage == null) {
			currentPage = 1;
		}
		int totalcount = messageService.countmessage(member.getMemberId());
		String url = "action=messagelist";
		int onepageCount = 10;
		String controller = "message";

		GeneratePaging generatePaging = new GeneratePaging();
		String paging = generatePaging.paging(currentPage, totalcount, url, onepageCount, ".parsingPage",controller);

		HashMap<String, String> listdata = new HashMap<String, String>();
		listdata.put("memberId", member.getMemberId());
		listdata.put("pageNo", String.valueOf(currentPage));

		List<Message> list = messageService.list(listdata);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("messageList",list);
		modelAndView.addObject("paging",paging);
		modelAndView.setViewName("group/messagelist");
		return modelAndView;
	}
	@RequestMapping(params = "action=messageview")
	public ModelAndView messageview(HttpServletRequest request, HttpServletResponse response,long messageNo) {
		ModelAndView modelAndView = new ModelAndView();
		
		Message message = (Message)messageService.view(messageNo);
		modelAndView.addObject("message",message);
		modelAndView.setViewName("group/messageView");
		
		return modelAndView;
	}
}
